#pragma once
#include <geometry\primitive.h>
#include "rigid_body.h"
#include <device_launch_parameters.h>

namespace gc {
	///< update prim positions based on Newton physics; test prim-wall collision
	__global__ void updateDynamics(int size, SimpleSphere *prims, MPhys::RigidBody *rigids, Float dt, Point3f boxMin, Point3f boxMax) {
		int idx = blockIdx.x*blockDim.x + threadIdx.x;
		if (idx >= size) return;

		rigids[idx].move(dt);
		Point3f pos = rigids[idx].position, v = rigids[idx].v;
		Float e = rigids[idx].e_collision;
		Float radius = prims[idx].radius;
		// pull back if prim is outside of the scenebox
		if (pos.x - radius < boxMin.x) {
			pos.x = boxMin.x + radius + EPS;
			v.x = v.x*-e;
		}
		else if (pos.x + radius > boxMax.x) {
			pos.x = boxMax.x - radius - EPS;
			v.x = v.x*-e;
		}

		if (pos.y - radius < boxMin.y) {
			pos.y = boxMin.y + radius + EPS;
			v.y = v.y*-e;
		}
		else if (pos.y + radius > boxMax.y) {
			pos.y = boxMax.y - radius - EPS;
			v.y = v.y*-e;
		}

		if (pos.z - radius < boxMin.z) {
			pos.z = boxMin.z + radius + EPS;
			v.z = v.z*-e;
		}
		else if (pos.z + radius > boxMax.z) {
			pos.z = boxMax.z - radius - EPS;
			v.z = v.z*-e;
		}

		rigids[idx].position = pos;
		rigids[idx].v = v;
		prims[idx].center = pos;
	}
}